package edu.hainnu.niit.service.Impl;

import edu.hainnu.niit.dao.ProductDao;
import edu.hainnu.niit.model.PageBean;
import edu.hainnu.niit.model.Product;
import edu.hainnu.niit.service.IProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @package_name edu.hainnu.niit.service.Impl
 * @auther GGB
 * @date 2021/12/12
 * @time 13:45
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Resource
    ProductDao productDao;

    @Override
    public List<Product> productService(Integer page) {
        List<Product> productList = productDao.queryProduct(page);
        return productList;
    }

    @Override
    public List<Product> Is_goodService(Integer page) {
        List<Product> productList = productDao.queryByIs_good(page);
        return productList;
    }

    @Override
    public List<Product> Is_goodService2(Integer page) {
        List<Product> productList = productDao.queryByIs_good2(page);
        return productList;
    }

    @Override
    public PageBean<Product> queryProductsService(Integer currentPage, Integer pageSize,String sname) {
        PageBean<Product> pageBean =new PageBean<Product>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int Count = productDao.queryProductTotal(sname);
        pageBean.setTotalCount(Count);

        int totalPage = Count % pageSize == 0 ? Count / pageSize :(Count / pageSize) + 1 ;
        pageBean.setTotalPage(totalPage);

        currentPage=(currentPage-1)*pageSize;
        pageBean.setList(productDao.queryProducts(currentPage,pageSize,sname));
        return pageBean;
    }

    @Override
    public Product queryProductByIDService(Integer id) {
        productDao.ProductCountadd(id);
        return productDao.queryProductByID(id);
    }
}
